<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               v-model="form"
               ref="crud"
               @search-change="searchChange"
               @search-reset="searchReset"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="primary"
                   size="small"
                   v-if="permission.batchOrder_add"
                   icon="el-icon-plus"
                   plain
                   @click="goToOrderItem">集采新增
        </el-button>
      </template>
      <template #menu="{size,row,index}">
        <el-button @click="openDetail(row,index)"
                   text
                   icon="el-icon-view"
                   type="text"
                   :size="size">查看/审核</el-button>
        <el-button v-if="(row.status ===3 || row.status ===6) && permission.batchOrder_add && row.companyId === loginUserInfo.companyId" @click="itemEdit(row,index)"
                   text
                   type="text"
                   icon="el-icon-edit"
                   :size="size">集采修改</el-button>
      </template>
      <template slot-scope="{row}"
                slot="status">
        <el-tag v-if="row.status ===1 || row.status ===4" type="warning">{{ statusDicData[row.status].label }}</el-tag>
        <el-tag v-if="row.status ===2 || row.status ===5" type="success">{{ statusDicData[row.status].label }}</el-tag>
        <el-tag v-if="row.status ===3 || row.status ===6" type="danger">{{ statusDicData[row.status].label }}</el-tag>
      </template>
    </avue-crud>
    <batch-order-detail  ref="batch-order-detail" :batchOrderId="batchOrderId" :status="rowStatus" @onConfirm="onLoad(page)"></batch-order-detail>
  </basic-container>
</template>

<script>
  import {getBatchList} from "@/api/business/batchOrderItem";
  import {getList} from "@/api/business/batchOrder";
  import batchOrderDetail from "./batchOrderDetail";
  import {mapGetters} from "vuex";

  export default {
    components: { batchOrderDetail },
    data() {
      return {
        batchOrderId: '',
        rowStatus: 1,
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        option: {
          emptyBtn: false,
          addBtn: false,
          refreshBtn: false,
          searchShowBtn: false,
          columnBtn: false,
          height:'auto',
          align:'center',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          indexLabel: '序号',
          viewBtn: false,
          editBtn: false,
          delBtn: false,
          dialogClickModal: false,
          column: [
            {
              label: "批次",
              prop: "batchId",
              type: "select",
              dicData: [],
              props: {
                label: "batchName",
                value: "id"
              },
              search: true,
            },
            {
              label: "医药机构名称",
              prop: "instName",
              type: "input",
              search: true,
              searchLabelWidth: 120,
              searchSpan: 8,
            },
            {
              label: "企业名称",
              prop: "companyName",
              type: "input",
            },
            {
              label: "申报时间",
              prop: "createTime",
              type: "input",
            },
            {
              label: "状态",
              prop: "status",
              type: "input",
              slot: true,
            },
            {
              label: "更新时间",
              prop: "updateTime",
              type: "input",
            }
          ]
        },
        statusDicData: [
          {
            label: '未知',
            value: 0
          },
          {
            label: '待审核',
            value: 1
          },
          {
            label: '医药机构审核通过',
            value: 2
          },
          {
            label: '医药机构审核不通过',
            value: 3
          },
          {
            label: '待医保局审核',
            value: 4
          },
          {
            label: '医保局审核通过',
            value: 5
          },
          {
            label: '医保局审核不通过',
            value: 6
          }
        ],
        data: []
      };
    },
    created() {
      console.log("role_name",this.userInfo.role_name);
      getBatchList().then(res => {
        this.findObject(this.option.column, "batchId").dicData = res.data.data;
      });
      if(this.userInfo.role_name==="medical"){
        this.findObject(this.option.column, "instName").searchDisabled=true;
        this.findObject(this.option.column, "instName").searchValue=this.loginUserInfo.institution.instName;
        this.$set(this.query,"instName",this.loginUserInfo.institution.instName);
      }
      if(this.userInfo.role_name==="districtAdmin"){
        this.$set(this.query,"district",this.loginUserInfo.dept.remark)
      }
      if(this.userInfo.role_name==="distribution"){
        this.$set(this.query,"companyId",this.loginUserInfo.companyId);
      }
    },
    computed: {
      ...mapGetters(["userInfo","permission","loginUserInfo"])
    },
    methods: {
      goToOrderItem() {
        this.$router.push({path: `/business/batchOrderItem`});
      },
      itemEdit(row) {
        this.$router.push({path: `/business/batchOrderItemEdit`,
          query: {batchOrderId: row.id, batchId: row.batchId, batchName: row.batchName,instName: row.instName, companyName: row.companyName, remark: row.remark}});
      },
      openDetail(row) {
        this.batchOrderId = row.id;
        this.rowStatus = row.status;
        this.$refs["batch-order-detail"].query = {
          batchName: row.batchName,
          instName: row.instName,
          companyName: row.companyName
        };
        this.$refs["batch-order-detail"].visible = true;

      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
        });
      }
    }
  };
</script>

<style>
</style>
